unit UPosition;

interface

uses Types, IniFiles;

const CWndPosition = '%s.position';

type
  TPIntegerDynArray = array of PInteger;

  TFormPosition = record {.$DEFINE STUPID_JOB}{turned off by default}
  strict private {$IFNDEF STUPID_JOB} const
    CLeft= 'left';
    CTop = 'top';
    CRight = 'right';
    CBottom = 'bottom';
    CFullScreen = 'full_screen';
    CPositions: array [0 .. 3] of string = (CLeft, CTop, CRight, CBottom);
  {$REGION 'U foolish env, it''s all because of you!'}{$ELSE}
    CLeft, CTop, CRight, CBottom, CFullScreen: string
    CPositions: array of string;
  {$ENDIF}{$ENDREGION}
  public
    FullScreen: boolean;
    function LoadFromFile(f: TIniFile; const Section: string): TFormPosition;
    procedure SaveToFile(f: TIniFile; const Section: string);
    procedure Assign(ALeft, ATop, AWidth, AHeight: integer);
    function GetArray: TPIntegerDynArray;
    case integer of
      0: (Left, Top, Right, Bottom: integer);
      1: (LeftTop, RightRottom: TPoint);
      2: (Rect: TRect);
  end;

implementation

{ TPosition }

procedure TFormPosition.Assign(ALeft, ATop, AWidth, AHeight: integer);
begin
  Left := ALeft;
  Top := ATop;
  Right := AWidth;
  Bottom := AHeight;
end;

function TFormPosition.GetArray: TPIntegerDynArray;
begin
  Result := TPIntegerDynArray.Create(@Left, @Top, @Right, @Bottom);
end;

function TFormPosition.LoadFromFile(f: TIniFile; const Section: string): TFormPosition;
var
  i: integer;
  p: TPIntegerDynArray;
begin
  p := GetArray;
  for i := 0 to 3 do
    p[i]^ := f.ReadInteger(Section, CPositions[i], 100);
  FullScreen := f.ReadBool(Section, CFullScreen, False);
  Result := self;
end;

procedure TFormPosition.SaveToFile(f: TIniFile; const Section: string);
var
  i: integer;
  p: TPIntegerDynArray;
begin
  p := GetArray;
  for i := 0 to 3 do
    f.WriteInteger(Section, CPositions[i], p[i]^);
  f.WriteBool(Section, CFullScreen, FullScreen)
end;


end.
